package com.study.living;

import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import java.util.ArrayList;
import java.util.List;

public class BaseActivity extends AppCompatActivity {
    
    private static final String TAG = "Living";

    /**
     * 任务，在Activity可见时执行，常用于onRequestPermissionsResult、onActivityResult。
     *
     * 1、权限结果返回，Activity周期：onRequestPermissionsResult -》 onResume。
     * 2、onActivityResult  -》 onResume。
     * 避免在结果返回时，视图不可见，做dialog提交、或者其他操作等
     */
    private List<Runnable> mTask;

    /**
     * 是否对用户可见
     */
    private boolean isVisibleToUser = false;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(TAG, "onCreate: ");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.i(TAG, "onStart: ");
    }

    @Override
    protected void onResume() {
        super.onResume();
        isVisibleToUser = true;
        Log.i(TAG, "onResume: ");
    }

    @Override
    protected void onPause() {
        super.onPause();
        isVisibleToUser = false;
        Log.i(TAG, "onPause: ");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.i(TAG, "onStop: ");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "onDestroy: ");
    }


    /**
     * 请求权限
     *
     * @param permissions
     * @param requestCode
     */
    public void requestDangerousPermissions(String[] permissions, int requestCode) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            handlePermissionResult(true, requestCode);
            return;
        }
        List<String> needPermissions = new ArrayList<>();
        for (String permission : permissions) {
            if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED ||
                    ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) {
                needPermissions.add(permission);
            }
        }
        if (needPermissions.size() == 0) {
            handlePermissionResult(true, requestCode);
            return;
        }
        ActivityCompat.requestPermissions(this, needPermissions.toArray(new String[needPermissions.size()]), requestCode);
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        boolean isGranted = true;
        for (int result : grantResults) {
            if (result != PackageManager.PERMISSION_GRANTED) {
                isGranted = false;
            }
        }
        boolean finish = handlePermissionResult(isGranted, requestCode);
        if (!finish){
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }


    /**
     * 处理请求危险权限的结果
     * @param isGranted 是否允许
     * @param requestCode
     * @return
     */
    public boolean handlePermissionResult(boolean isGranted, int requestCode) {
        return false;
    }


    /**
     * 请求权限
     *
     * @param permissions
     */
    public boolean hasDangerousPermissions(String[] permissions) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            return true;
        }
        List<String> needPermissions = new ArrayList<>();
        for (String permission : permissions) {
            if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED || ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) {
                needPermissions.add(permission);
            }
        }
        if (needPermissions.size() == 0) {
            return true;
        }
        return false;
    }

    public void runTaskOnVisibleToUser(Runnable runnable){
        if (runnable != null){
            if (isVisibleToUser){
                runnable.run();
            } else {
                if (mTask == null){
                    mTask = new ArrayList<>();
                }
                mTask.add(runnable);
            }
        }
    }
}
